//
//  AppDelegate.m
//  UI09_UITabBarController
//
//  Created by LGC on 16/3/21.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "tabBarController.h"

#import "NaviOfRootFirstTabBarItemViewController.h"
#import "VCOfSecondTabBarItemViewController.h"
#import "VCOfThirdTabBarItem.h"
#import "VCOf4thTabBarItem.h"
#import "VCOf5thTabBarItem.h"
#import "VCOf6thTabBarItem.h"

@interface AppDelegate () <UITabBarControllerDelegate
>

@end

@implementation AppDelegate

-(void)dealloc {
    [_window release];
    [super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    [self createTabBarController];
    
 
    [_window release];
    return YES;
}

#pragma mark - 知识点1:UITabBarController

- (void)createTabBarController {
    // 创建UITabBar对象.
    tabBarController *rootTabBar = [[tabBarController alloc]init];
    
    // 设置root的viewControllers属性.
    rootTabBar.viewControllers = [self createRootOfVCs];
    
    rootTabBar.delegate = self;
    
    self.window.rootViewController = rootTabBar;
    
    [rootTabBar release];


}

/** rootTabBar的viewControllers */
- (NSArray <UIViewController *> *)createRootOfVCs {
    
    NSMutableArray *arrOfVCs = [NSMutableArray array];
    
    // 第一个tabBarItem.
    ViewController *vc = [[ViewController alloc] init];
    
    NaviOfRootFirstTabBarItemViewController *navi = [[NaviOfRootFirstTabBarItemViewController alloc]initWithRootViewController:vc];
    
    navi.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:101];
    navi.tabBarItem.badgeValue = @"+99";

    
    [arrOfVCs addObject:navi];
    
    [vc release];
    [navi release];
    
    // 第二个tabBarItem.
    VCOfSecondTabBarItemViewController *item2 = [[VCOfSecondTabBarItemViewController alloc]init];
    
    item2.tabBarItem = [[[UITabBarItem alloc]initWithTitle:@"乌龟" image:[UIImage imageNamed:@"iconfont-untitled.png"] tag:102]autorelease];
    
    item2.tabBarItem.badgeValue = @"+99";
    
    [arrOfVCs addObject:item2];
    
    [item2 release];
    
    // 第三个tabBarItem.
    VCOfThirdTabBarItem *item3 = [[VCOfThirdTabBarItem alloc]init];
    item3.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:103];
    [arrOfVCs addObject:item3];
    [item3 release];
    
    // 第四个tabBarItem.
    VCOf4thTabBarItem *item4 = [[VCOf4thTabBarItem alloc]init];
    item4.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemSearch tag:104];
    [arrOfVCs addObject:item4];
    [item4 release];
    
    // 第五个tabBarItem.
    VCOf5thTabBarItem *item5 = [[VCOf5thTabBarItem alloc]init];
    item5.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:105];
    [arrOfVCs addObject:item5];
    [item5 release];
    
    // 第六个tabBarItem.
    VCOf6thTabBarItem *item6 = [[VCOf6thTabBarItem alloc]init];
    item6.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:106];
    [arrOfVCs addObject:item6];
    [item6 release];
    
    
    return arrOfVCs;

}

#pragma mark - 知识点2: UITabBarControllerDelegate
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    
    // 选择第几个item时, 第几个item角标消失.
    viewController.tabBarItem.badgeValue = nil;
   
    NaviOfRootFirstTabBarItemViewController *navi = tabBarController.viewControllers[0];
    
    [navi retain];
    if (navi.topViewController != navi.viewControllers[0]) {
        
        // 重新修改navi的VC数组, 只有一个VC.
        navi.viewControllers = @[navi.viewControllers[0]];

    }
   
    [navi release];
    
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
